from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from .ser import *
from django.core.paginator import Paginator


# Create your views here.


class JournalismM(APIView):
    def post(self, request):
        gain = JournalismSer(data=request.data)
        if gain.is_valid():
            gain.save()
            return Response({'code': 200})
        return Response({'code': 10010})

    def get(self, request):
        gain = Journalism.objects.all()
        p = request.GET.get('page')
        page = Paginator(gain, 2)
        result = page.page(p)
        ser = JournalismSer(result, many=True)
        return Response({'code': 200, 'matter': ser.data, 'tpages': page.num_pages})


class AdvertisingM(APIView):
    def post(self, request):
        gain = AdvertisingSer(data=request.data)
        if gain.is_valid():
            gain.save()
            return Response({'code': 200})
        return Response({'code': 10010})

    def get(self, request):
        gain = Advertising.objects.all()
        p = request.GET.get('p')
        page = Paginator(gain, 2)
        result = page.page(p)
        ser = AdvertisingSer(result, many=True)
        return Response({'code': 200, 'matter': ser.data, 'tpages':page.num_pages})
